import { defineStore } from 'pinia';
import axios from 'axios';
import { ref } from 'vue';
import { 
  getQuestionList, 
  getQuestionDetail, 
  getHotQuestions, 
  getWaitingQuestions,
  getRecommendQuestions,
  getFollowingQuestions,
  createQuestion as apiCreateQuestion
} from '../api/question';

export const useQuestionStore = defineStore('question', () => {
  // 问题列表
  const questions = ref([]);
  // 问题详情
  const questionDetail = ref(null);
  // 热门问题
  const hotQuestions = ref([]);
  // 等待回答的问题
  const waitingQuestions = ref([]);
  // 列表分页
  const pagination = ref({
    current: 1,
    size: 10,
    total: 0
  });
  // 加载状态
  const loading = ref(false);
  // 查询条件
  const query = ref({
    categoryId: null,
    keyword: '',
    status: null
  });
  
  // 获取问题列表
  const fetchQuestions = async (params) => {
    try {
      loading.value = true;
      
      // 合并查询条件
      const queryParams = {
        current: pagination.value.current,
        size: pagination.value.size,
        ...query.value,
        ...params
      };
      
      const res = await getQuestionList(queryParams);
      
      questions.value = res.data.records;
      pagination.value.current = res.data.current;
      pagination.value.size = res.data.size;
      pagination.value.total = res.data.total;
      
      return res.data;
    } finally {
      loading.value = false;
    }
  };
  
  // 获取问题详情
  const fetchQuestionDetail = async (id) => {
    try {
      loading.value = true;
      const res = await getQuestionDetail(id);
      questionDetail.value = res.data;
      return res.data;
    } finally {
      loading.value = false;
    }
  };
  
  // 获取热门问题
  const fetchHotQuestions = async () => {
    try {
      loading.value = true;
      const res = await getHotQuestions({ 
        current: pagination.value.current,
        size: pagination.value.size 
      });
      questions.value = res.data.records;
      return res.data.records;
    } finally {
      loading.value = false;
    }
  };
  
  // 获取等待回答的问题
  const fetchWaitingQuestions = async () => {
    try {
      loading.value = true;
      const res = await getWaitingQuestions({ 
        current: pagination.value.current,
        size: pagination.value.size 
      });
      waitingQuestions.value = res.data.records;
      return res.data.records;
    } finally {
      loading.value = false;
    }
  };
  
  // 获取推荐问题
  const fetchRecommendQuestions = async () => {
    try {
      loading.value = true;
      const res = await getRecommendQuestions({ 
        current: pagination.value.current,
        size: pagination.value.size 
      });
      questions.value = res.data.records;
      return res.data.records;
    } finally {
      loading.value = false;
    }
  };
  
  // 获取关注用户的问题
  const fetchFollowingQuestions = async () => {
    try {
      loading.value = true;
      const res = await getFollowingQuestions({ 
        current: pagination.value.current,
        size: pagination.value.size 
      });
      questions.value = res.data.records;
      return res.data.records;
    } finally {
      loading.value = false;
    }
  };
  
  // 创建问题
  const createQuestion = async (data) => {
    try {
      loading.value = true;
      const res = await apiCreateQuestion(data);
      return res;
    } finally {
      loading.value = false;
    }
  };
  
  // 设置查询条件
  const setQuery = (newQuery) => {
    query.value = {
      ...query.value,
      ...newQuery
    };
  };
  
  // 重置查询条件
  const resetQuery = () => {
    query.value = {
      categoryId: null,
      keyword: '',
      status: null
    };
    pagination.value.current = 1;
  };
  
  return {
    questions,
    questionDetail,
    hotQuestions,
    waitingQuestions,
    pagination,
    loading,
    query,
    fetchQuestions,
    fetchQuestionDetail,
    fetchHotQuestions,
    fetchWaitingQuestions,
    fetchRecommendQuestions,
    fetchFollowingQuestions,
    createQuestion,
    setQuery,
    resetQuery
  };
});